import { Random, mock } from "mockjs"

// 缓存
let cache = {}

const randomDateTime = (startDate, endDate) => {
  var start = new Date(startDate).getTime();
  var end = new Date(endDate).getTime();
  var timestamp = Math.random() * (end - start) + start;
  return new Date(timestamp).toISOString().replace(/T/, ' ').replace(/\..+/, '');
}

// 创建数组
const createArr = (fn, cacheKey, length = 100) => {
  if (!length)
    length = Random.integer(100, 200)

  if (!cacheKey)
    cacheKey = fn.toString()
  if (cache[cacheKey]) return cache[cacheKey]

  cache[cacheKey] = new Array(length).fill().map(fn)
  return new Array(length).fill().map(fn)
}
// 返回列表
const createList = (arr, { pageNum, pageSize } = { pageNum: 1, pageSize: 10 }, attchData) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        rows: arr.slice((pageNum - 1) * pageSize, pageNum * pageSize),
        total: arr.length,
        ...attchData
      })
    }, Random.float(0, 1) * 1000)
  })
}
// 小区名
// const getParkName = () => Random.ctitle(3, 5) + Random.pick(['二期', '三期', '公寓', '小区'])
// const getParkName = () => Random.pick(["状元壹号", "书苑名家", "江南学府", "墨香楼", "学府春天", "枫林绿都", "四季云顶", "西山枫林", "七里香都", "满庭芳", "盛世豪庭", "嘉林景苑", "锦绣雷庭", "紫金苑", "中央公馆", "云海山庄", "洲际观天下"]) + Random.pick(['二期', '三期', '公寓', '小区'])
const getParkName = () => Random.pick(['暮辞小镇', '芋烤中心', '安寐轩', '疆野别墅', '留宸园', '天悦龙庭', '花间苑']) + Random.pick(['二期', '三期', '公寓', '小区'])


// 自定义模型
export const modleList = (query) => {
  const type2Name = {
    0: '小区',
    1: '楼栋',
    2: '单元',
    3: '住户',
  }
  return createList(
    createArr(() => {
      const useQuantity = Random.integer(0, 100)
      // 获取当前日期和时间
      const currentDate = new Date();

      // 生成今日时间的随机时间段
      const randomHour = Math.floor(Math.random() * (currentDate.getHours() + 1));
      const randomMinute = Math.floor(Math.random() * 60);
      const randomSecond = Math.floor(Math.random() * 60);
      const randomTime = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), randomHour, randomMinute, randomSecond);
      // 格式化日期和时间字符串
      const formattedTime = randomTime.toISOString().replace(/T/, ' ').replace(/\..+/, '');
      return {
        // modelName: Random.pick([
        //   '天然气长输管道及城镇燃气输配系统模型', '城市集中供热系统模型',
        //   '地源热泵模型', '区域供热系统模型', '民用采暖供热系统模型', '建筑热水供暖系统模型',
        //   "建筑蒸汽供暖系统模型", '建筑热风供暖系统模型', '太阳能供热采暖系统模型', '热泵机组模型',
        //   '二台SHL10-1.3-A锅炉房热水锅炉房模型', '燃煤锅炉模型'
        // ]),
        modelName: type2Name[query.listFlag] + '模型' + Random.character('upper'),
        createBy: Random.cname(),
        createTime: formattedTime,
        useQuantity,
        residentAreaStart: Random.integer(50, 100),
        residentAreaEnd: Random.integer(100, 150),
        houseType: Random.pick(['一居室', '二居室', '三居室']),
        radiatorType: Random.pick(['钢制散热器', "铝制散热器", '铜质散热器', '铜铝复合散热器']),
        childUseType: Random.integer(0, useQuantity),
        childUseQuantity: Random.integer(0, useQuantity),
        status: Random.pick([0, 1])
      }
    }, 'modleList-' + query.listFlag),
    query
  )
}
// 小区管理
export const plotList = (query) => {
  return createList(
    createArr(() => {
      const address = Random.county(true).split(' ')
      const province = address[0]
      const city = address[1]
      const area = address[2]
      return {
        id: Random.increment(),
        parkName: getParkName(),
        address,
        buildNumber: Random.integer(0, 20),
        heatUser: Random.integer(500, 1500),
        heatArea: Random.integer(100000, 10000000),
        areaPrice: Random.integer(10000, 20000),
        heatPrice: Random.float(2, 10),
        ownProperty: Random.ctitle(2, 4) + '物业',
        heatCompany:Random.pick(['中兴热力', '中全供热']),
        province,
        city,
        area,
        username:Random.cname(),
        tiaokongCeLue:Random.pick(['手动调控', "自动调控"]),
        createTime: randomDateTime("2023-01-01","2023-06-26"),
        jiliang: Random.pick(['面积计费', '按户计费']),
        mox:Random.pick(['模型X', "模型L",'模型O']),
      }
    }),
    query
  )
}
// 小区管理
export const buildList = (query) => {
  return createList(
    createArr(() => {
      const address = Random.county(true).split(' ')
      const province = address[0]
      const city = address[1]
      const area = address[2]
      const buildNumber = Random.integer(1, 20)
      const floorNumber = Random.integer(1, 30)
      const totalHouseNumber = floorNumber * Random.integer(1, 30)
      const heatingHouseNumber = Random.integer(totalHouseNumber * 0.2, totalHouseNumber)
      return {
        id: Random.increment(),
        parkName: getParkName(),
        address,
        buildNumber,
        buildName: buildNumber + '号楼',
        unitNumber: Random.integer(1, 4),
        floorNumber: floorNumber + '楼',
        heatingHouseNumber: heatingHouseNumber + '户',
        totalHouseNumber: totalHouseNumber + '户',
        heatUser: Random.integer(1000, 1000000),
        heatArea: heatingHouseNumber * Random.integer(60, 300),
        areaPrice: Random.integer(10, 20),
        heatPrice: Random.float(2, 10),
        heatMeter: Random.pick(['面积计费', '按户计费']),
        province,
        city,
        area,
        username:Random.cname(),
        createTime: randomDateTime("2023-01-01","2023-03-01"),
        genxinCreateTime: randomDateTime("2023-03-05","2023-06-25"),
        genxinUser:Random.cname(),
        jiliang: Random.pick(['面积计费', '按户计费']),
        mox:Random.pick(['模型X', "模型L",'模型O']),
      }
    }),
    query
  )
}
// 小区管理
export const unitBuildList = (query) => {
  return createList(
    createArr(() => {
      const address = Random.county(true).split(' ')
      const province = address[0]
      const city = address[1]
      const area = address[2]
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      const floorNumber = Random.integer(1, 30)
      const totalHouseNumber = floorNumber * Random.integer(1, 30)
      const heatingHouseNumber = Random.integer(totalHouseNumber * 0.2, totalHouseNumber)
      return {
        id: Random.increment(),
        lotName: getParkName(),
        parkName: getParkName(),
        address,
        buildNumber,
        buildName: buildNumber + '号楼',
        unitNumber,
        unitName: unitNumber + '单元',
        floorNumber: floorNumber + '楼',
        heatNumber: heatingHouseNumber + '户',
        totalHousehold: totalHouseNumber + '户',
        heatUser: Random.integer(1000, 1000000),
        heatArea: heatingHouseNumber * Random.integer(60, 300),
        areaPrice: Random.integer(10, 20),
        heatPrice: Random.float(2, 10),
        heatMeter: Random.pick(['面积计费', '按户计费']),
        remark: '',
        province,
        city,
        area,
        username:Random.cname(),
        createTime: randomDateTime("2023-01-01","2023-03-01"),
        genxinCreateTime: randomDateTime("2023-03-05","2023-06-25"),
        genxinUser:Random.cname(),
        jiliang: Random.pick(['面积计费', '按户计费']),
        mox:Random.pick(['模型X', "模型L",'模型O']),
      }
    }),
    query
  )
}
// 住户管理
export const householdList = (query) => {
  return createList(
    createArr(() => {
      const address = Random.county(true).split(' ')
      const province = address[0]
      const city = address[1]
      const area = address[2]
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      const floorNumber = Random.integer(1, 30)
      const totalHouseNumber = floorNumber * Random.integer(1, 30)
      const heatingHouseNumber = Random.integer(totalHouseNumber * 0.2, totalHouseNumber)
      const houseNumber = floorNumber + ('0' + Random.integer(1, 20)).slice(-2)
      return {
        id: Random.increment(),
        lotName: getParkName(),
        parkName: getParkName(),
        address,
        buildNumber,
        buildName: buildNumber + '号楼',
        unitNumber,
        unitName: unitNumber + '单元',
        floor: floorNumber + '楼',
        houseNumber,
        orientation: Random.pick(['东', '南', '西', '北']),
        pipePosition: Random.pick(['一级网管', '二级网管']),
        heatNumber: heatingHouseNumber + '户',
        totalHousehold: totalHouseNumber + '户',
        heatUser: Random.integer(1000, 1000000),
        heatArea: Random.integer(60, 300),
        hotCardNumber: ("00000" + houseNumber).slice(-6),
        userName: Random.cname(),
        landPhone: Random.pick(['8', '6', '2']) + mock(/\d{7,7}/),
        mobilePhone: Random.pick(['13', '17', '18']) + mock(/\d{9,9}/),
        email: Random.email(),
        heatingType: Random.pick(['民用']),
        province,
        city,
        area,
        type:Random.pick(['普通用户','老年用户']),
        username:Random.cname(),
        createTime: randomDateTime("2023-01-01","2023-03-01"),
        genxinCreateTime: randomDateTime("2023-03-05","2023-06-25"),
        genxinUser:Random.cname(),
        jiliang: Random.pick(['面积计费', '按户计费']),
        mox:Random.pick(['模型X', "模型L",'模型O']),
      }
    }),
    query
  )
}
// 住户分类
export const householdClassifyList = (query) => {
  return createList(
    createArr(() => {
      return {
        id: Random.increment(),
        plotId: Random.increment(),
        plotName: getParkName(),
        // classifyName: Random.ctitle(2, 4),
        classifyName: Random.pick(['普通用户', "老年用户"]),
        parkName: getParkName(),
        username:Random.pick(['李明烟', "戚齐彭", '凤臻里', '方琥劲','任松敬','阮帆喜']),
        createTime: randomDateTime("2023-01-01","2023-06-26"),
        // miaoshu:Random.pick([''])
      }
    }),
    query
  )
}
// 设备分组
export const equipmentList = (query) => {
  return createList(
    createArr(() => {
      // 获取当前日期和时间
      const currentDate = new Date();

      // 生成今日时间的随机时间段
      const randomHour = Math.floor(Math.random() * (currentDate.getHours() + 1));
      const randomMinute = Math.floor(Math.random() * 60);
      const randomSecond = Math.floor(Math.random() * 60);
      const randomTime = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), randomHour, randomMinute, randomSecond);
      // 格式化日期和时间字符串
      const formattedTime = randomTime.toISOString().replace(/T/, ' ').replace(/\..+/, '');
      return {
        lotId: getParkName(),
        groupName: Random.pick([
          '楼栋设备组', '单元设备组', '住户设备组',
        ]),
        createBy: Random.cname(),
        createTime: formattedTime,
      }
    }),
    query
  )
}

// 设备管理数据
export const equipmentMessageList = (query) => {
  return createList(
    createArr(() => {
      const lotName = getParkName();
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      // 获取当前日期和时间
      const currentDate = new Date();

      // 生成今日时间的随机时间段
      const randomHour = Math.floor(Math.random() * (currentDate.getHours() + 1));
      const randomMinute = Math.floor(Math.random() * 60);
      const randomSecond = Math.floor(Math.random() * 60);
      const randomTime = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), randomHour, randomMinute, randomSecond);
      // 格式化日期和时间字符串
      const formattedTime = randomTime.toISOString().replace(/T/, ' ').replace(/\..+/, '');
      return {
        lotName: getParkName(),
        buildName: buildNumber + '号楼',
        unitName: unitNumber + '单元',
        householdName: Random.cname(),
        equipmentName:  Random.pick(["单元阀门", "户用阀门", '单元热量表','户用热量表','室温采集器']),
        equipmentCode: (mock(/\d{7,7}/) + buildNumber + unitNumber).slice(-10),
        equipmentType: Random.pick(["阀门", "户表", '室温采集器']),
        address: Random.county(true),
        autoRead: Random.pick(['是', '否']),
        equipmentGroupId: Random.pick([
          '楼栋设备组', '单元设备组', '住户设备组',
        ]),
        remark: '',
        createBy: Random.cname(),
        createTime: formattedTime,
      }
    }),
    query
  )
}

// 设备管理数据
export const equipmentListInfo = (query) => {
  return createList(
    createArr(() => {
      const lotName = getParkName();
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      return {
        lotName: getParkName(),
        buildName: buildNumber + '号楼',
        unitName: unitNumber + '单元',
        householdName: Random.integer(1, 20),
        equipmentName: lotName + '设备',
        equipmentCode: (mock(/\d{7,7}/) + buildNumber + unitNumber).slice(-10),
        equipmentType: Random.pick(["阀门", "户表", '室温采集器']),
        address: Random.county(true),
        autoRead: Random.pick(['是', '否']),
        equipmentGroupId: Random.pick([
          '楼栋设备组', '单元设备组', '住户设备组',
        ]),
        xiangxiweizhi: Random.pick([
          '水井房', '一楼拐角处',
        ]),
        remark: '',
        createBy: Random.cname(),
        createTime: Random.integer(2000, 2023) + Random.datetime('-MM-dd HH:mm:ss'),
      }
    }),
    query
  )
}



// 数据列表
export const dataList = (query) => {
  return createList(
    createArr(() => {
      const lotName = getParkName();
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      // 获取当前日期和时间
      const currentDate = new Date();

      // 生成今日时间的随机时间段
      const randomHour = Math.floor(Math.random() * (currentDate.getHours() + 1));
      const randomMinute = Math.floor(Math.random() * 60);
      const randomSecond = Math.floor(Math.random() * 60);
      const randomTime = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), randomHour, randomMinute, randomSecond);
      // 格式化日期和时间字符串
      const formattedTime = randomTime.toISOString().replace(/T/, ' ').replace(/\..+/, '');
      return {
        plotName: getParkName(),
        buildName: buildNumber + '号楼',
        unitName: unitNumber + '单元',
        household: Random.pick([ Random.integer(1000, 1008), Random.integer(2000, 2008), Random.integer(3000, 3008), Random.integer(4000, 4008), Random.integer(5000, 5008), Random.integer(6000, 6008)]),
        louceng: Random.integer(1, 30),
        householdName: Random.cname(),
        thermalPower: Random.float(0, 2, 2, 2) + 'kw',
        velocityFlow: Random.float(0, 1, 4, 4) + 'm³/h',
        waterSupplyTemperature: Random.float(23, 41, 1, 2) + '℃',
        returnWaterTemperature: Random.float(23, 41, 1, 2) + '℃',
        totalFlow: Random.float(100, 1000, 1, 2) + 'm³',
        currentHeatOrCold: Random.float(1000, 10000, 1, 2) + 'kwh',
        setOpen: Random.float(10, 90, 1, 2) + '%',
        valveCrateTime: formattedTime,
        thermalCreateTime: formattedTime,
        shiwenTime: formattedTime,
        shiwen: Random.float(23, 41, 1, 2) + '℃',
      }
    }, 'dataList-' + query.listFlag),
    query,
    { code: 200 }
  )
}

// 设备告警
export const warningList = (query) => {
  return createList(
    createArr(() => {
      const lotName = getParkName();
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      // 获取当前日期和时间
      const currentDate = new Date();

      // 生成今日时间的随机时间段
      const randomHour = Math.floor(Math.random() * (currentDate.getHours() + 1));
      const randomMinute = Math.floor(Math.random() * 60);
      const randomSecond = Math.floor(Math.random() * 60);
      const randomTime = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), randomHour, randomMinute, randomSecond);
      // 格式化日期和时间字符串
      const formattedTime = randomTime.toISOString().replace(/T/, ' ').replace(/\..+/, '');
      return {
        plotName: getParkName(),
        lou: buildNumber + '号楼',
        dan: unitNumber + '单元',
        hu: Random.integer(1, 10),
        she: Random.pick(['单元阀门','户用阀门','单元热量表','户用热量表','室温采集器']),
        // she: Random.cname(),
        num: (mock(/\d{7,7}/) + buildNumber + unitNumber).slice(-10),
        leix: Random.pick(['故障告警', '传感器告警', '数据异常告警']),
        // dengji: Random.pick(['红色告警', '橙色告警', '黄色告警', '蓝色告警']),
        dengji: Random.pick(['一般', '严重', '紧急']),
        miaoshu: '',
        shijian: Random.pick(['2023-06-25']) + Random.datetime(' HH:mm:ss'),
        type: Random.pick([0, 1]),
        chuliTime: formattedTime,
      }
    }),
    query
  )
}

// 设备告警
export const warnSettingList = (query) => {
  return createList(
    createArr(() => {
      const lotName = getParkName();
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      // 获取当前日期和时间
      const currentDate = new Date();

      // 生成今日时间的随机时间段
      const randomHour = Math.floor(Math.random() * (currentDate.getHours() + 1));
      const randomMinute = Math.floor(Math.random() * 60);
      const randomSecond = Math.floor(Math.random() * 60);
      const randomTime = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), randomHour, randomMinute, randomSecond);
      // 格式化日期和时间字符串
      const formattedTime = randomTime.toISOString().replace(/T/, ' ').replace(/\..+/, '');
      return {
        plotName: getParkName(),
        lou: buildNumber + '号楼',
        dan: unitNumber + '单元',
        hu: Random.cname(),
        she: lotName + '设备',
        she: Random.cname(),
        num: (mock(/\d{7,7}/) + buildNumber + unitNumber).slice(-10),
        gaojType: Random.pick(['故障告警', '传感器告警', '数据异常告警']),
        // gaojDengji: Random.pick(['红色告警', '橙色告警', '黄色告警', '蓝色告警']),
        gaojDengji: Random.pick(['一般', '严重', '紧急']),
        chuangjianren: Random.cname(),
        shijian: formattedTime,
        tuisong: Random.pick([true, false]),
        miaoshu: ''
      }
    }),
    query
  )
}


// 设备告警
export const balanceList = (query) => {
  return createList(
    createArr(() => {
      const lotName = getParkName();
      const floorNumber = Random.integer(1, 30)
      const buildNumber = Random.integer(1, 20)
      const unitNumber = Random.integer(1, 4)
      return {
        plotName: getParkName(),
        buildName: buildNumber + '号楼',
        unit: unitNumber + '单元',
        floor: floorNumber + '楼',
        household: Random.pick([ Random.integer(1000, 1008), Random.integer(2000, 2008), Random.integer(3000, 3008), Random.integer(4000, 4008), Random.integer(5000, 5008), Random.integer(6000, 6008)]),
        equipmentName: lotName + '设备',
        equipmentCode: (mock(/\d{7,7}/) + buildNumber + unitNumber).slice(-10),
        status: Random.pick(['在线', '离线']),
        valveOpen: Random.float(10, 90, 1, 2) + '%',
        gong: Random.float(23, 41, 1, 2) + '℃',
        hui: Random.float(23, 41, 1, 2) + '℃',
        mox:Random.pick(['模型D', '模型G','模型H','模型H']),
        postion:Random.pick(['室内','水井房']),
        shebeiNum:Random.integer(1, 20),
        shebeiZc:Random.integer(1, 15),
        shebeiYc:Random.integer(1, 5),
        loudongNum:Random.integer(2, 10),
        danyuanNum:Random.integer(2, 6),
      }
    }, 'balanceList-' + query.listFlag),
    query
  )
}


export const dataanalyze = (query) => {
  // const buildNum = Random.integer(1, 20)
  const buildNum = 6
  return createList(
    [],
    query,
    {
      ring: createArr((v, i) => {
        return {
          value: Random.integer(10, 400),
          name: i + 1 + '号楼',
        }
      }, 'ring', buildNum),
      line: {
        power: createArr(() => Random.float(0, 2, 2, 2), 'line-power', 10),
        temperature: createArr(() => Random.float(23, 41, 1, 2), 'line-temperature', 10),
        flow: createArr(() => Random.float(0, 1, 4, 4), 'line-flow', 10)
      },
      bar1: {
        x: createArr((v, i) => i + 1 + '号楼', 'bar-power-x', buildNum),
        data: createArr(() => Random.float(0, 2, 2, 2), 'bar-power-data', buildNum)
      },
      bar2: {
        x: createArr((v, i) => i + 1 + '号楼', 'bar-temperature-x', buildNum),
        data: createArr(() => Random.float(23, 41, 1, 2), 'bar-temperature-data', buildNum)
      },
      bar3: {
        x: createArr((v, i) => i + 1 + '号楼', 'bar-flow-x', buildNum),
        data: createArr(() => Random.float(0, 2, 2, 2), 'bar-flow-data', buildNum)
      },
    }
  )
}
